package com.scw.action;



import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.scw.service.UserService;
import com.scw.util.CommonNameUtil;
import com.scw.util.MD5Util;

import com.opensymphony.xwork2.ActionContext;

import com.scw.model.SysUser;

/**
 * 
 * @Description 登录管理类
 * @author 王华聪
 * @version v 1.0
 */
public class LoginAction {
	private SysUser sysUser;
	private int pageSize=1;
	private String vcode;//验证码
	private UserService userService;
	private String url;
	private boolean autologin;//自动登陆
	
	public String checkLogin(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response =ServletActionContext.getResponse();
		HttpSession session = request.getSession();
		String msg="";
		if(vcode != null && !"".equals(vcode) && session.getAttribute("checkCode").toString().toLowerCase().equals(vcode.toLowerCase())){
			sysUser.setPassword(new MD5Util(sysUser.getPassword()).compute());
			sysUser = userService.login(sysUser);
			if(sysUser.getId()!= null){
				ActionContext ac = ActionContext.getContext();
				ac.getSession().put(CommonNameUtil.SessionUser, sysUser);
				CommonNameUtil.onlineList.put(sysUser.getUser_name(), sysUser);
				//自动登陆
				if(autologin){
					Cookie cookie=new Cookie("autologin",sysUser.getUser_name()+":"+sysUser.getPassword());
					cookie.setMaxAge(7*24*60*60);//7天
					cookie.setPath(request.getContextPath());
					response.addCookie(cookie);
				}
				//登录完跳转的页面
				url=(String) ac.getSession().get(CommonNameUtil.sessionNextAction);
				if(url!= null &&!"".equals(url)){
					session.setAttribute(CommonNameUtil.sessionNextAction, null);
					return "url";
				}
				return "success";
			}else{
				msg="用户名或密码错误";
			}
		}else{
			msg="验证码错误";
		}
		request.setAttribute("msg", msg);
		return "login";
	}
	/**
	 * 退出
	 */
	public String quit(){
		HttpServletRequest req=ServletActionContext.getRequest();
		HttpServletResponse res=ServletActionContext.getResponse();
		HttpSession session=req.getSession();
		String id=req.getParameter("id");
		sysUser=userService.get(Long.valueOf(id));
		session.setAttribute(CommonNameUtil.SessionUser, null);
		Cookie cookie=new Cookie("autologin",null);
		cookie.setMaxAge(0);
		cookie.setPath(req.getContextPath());
		res.addCookie(cookie);
		CommonNameUtil.onlineList.remove(sysUser.getUser_name());
		req.setAttribute("msg", "恭喜！退出成功");
		return "ok";
	}
	
	public SysUser getSysUser() {
		return sysUser;
	}


	public void setSysUser(SysUser sysUser) {
		this.sysUser = sysUser;
	}


	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	
	public String getVcode() {
		return vcode;
	}


	public void setVcode(String vcode) {
		this.vcode = vcode;
	}


	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	public boolean isAutologin() {
		return autologin;
	}
	public void setAutologin(boolean autologin) {
		this.autologin = autologin;
	}
	
	
}
